package com.ac.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import darabonba.core.client.ClientOverrideConfiguration;

import java.math.BigDecimal;

/**
 * @Author gjj
 * @date 2022-12-20
 */
public class AliyunSmsBase {

    /** 正式id和secret */

//    public static final String ACCESSKEY_ID = "";
//    public static final String ACCESSKEY_SECRET = "";

    /** 测试id和secret */
    public static final String ACCESSKEY_ID = "";
    public static final String ACCESSKEY_SECRET = "";

    /** 云通信短信API产品*/
    public static final String PRODUCT = "Dysmsapi";
    /** 产品域名*/
    public static final String DOMAIN = "dysmsapi.aliyuncs.com";


    /** 短信签名 */
    public static final String SIGN_NAME  = "";
    /** 短信模板 */
    public static final String RGEX  = "\\$\\{(.*?)\\}";
    /** 每次最多发送100条 */
    public static final Integer LIMIT_SIZE = 100;
    /** 短信价格 */
    public static final BigDecimal SMS_MONEY = new BigDecimal("0.043");
    /** 外部调用默认申请单id */
    public static final Integer APPLY_ID = 0;
    /** 平台推送 */
    public static final String PLATFORM_CHANNEL = "1";
    /** 接口推送 */
    public static final String INTERFACE_CHANNEL = "2";

    /**
     * 外部调用状态码
     */
    //成功调用
    public static final Integer OK = 0;
    //阿里云调用错误
    public static final Integer alibabaCloudError = 201;
    //token无效或过期
    public static final Integer tokenError= 202;
    //外部系统不存在或未启用
    public static final Integer authorizeError = 203;
    //变量为空
    public static final Integer variableError = 204;
    //模板不存在
    public static final Integer templateError = 205;
    //未知错误
    public static final Integer unknowError = 206;

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     *
     */
    public static AsyncClient createClient(String accessKeyId, String accessKeySecret)  {
        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                //.securityToken("<your-token>") // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
        return client;
    }

    /**
     * 使用默认账号Client
     * @return Client
     *
     */
    public static AsyncClient createClient()  {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ACCESSKEY_ID)
                .accessKeySecret(ACCESSKEY_SECRET)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        return client;
    }
}
